package org.jeecg.modules.zhima.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.zhima.admin.entity.ZmPlayer;
import org.jeecg.modules.zhima.admin.entity.ZmPlayerRanking;
import org.jeecg.modules.zhima.admin.service.IZmPlayerService;
import org.jeecg.modules.zhima.center.entity.UmsMember;
import org.jeecg.modules.zhima.center.service.IUmsMemberService;
import org.jeecg.modules.zhima.portal.domain.Player;
import org.jeecg.modules.zhima.portal.enums.PlayerLevel;
import org.jeecg.modules.zhima.portal.service.IPlayerRankingService;
import org.jeecg.modules.zhima.portal.service.IPlayerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class PlayerServiceImpl implements IPlayerService {

    @Autowired
    private IZmPlayerService zmPlayerService;
    @Autowired
    private IUmsMemberService umsMemberService;
    @Autowired
    private IPlayerRankingService playerRankingService;

    @Override
    public Player getById(String id) {
        ZmPlayer zmPlayer = zmPlayerService.getById(id);
        return fill(zmPlayer);
    }

    @Override
    public Player getByUserId(String userId) {
        ZmPlayer zmPlayer = zmPlayerService.getOne(new LambdaQueryWrapper<ZmPlayer>().eq(ZmPlayer::getUserId, userId));
        if (zmPlayer == null){
            return null;
        }
        return fill(zmPlayer);
    }

    @Override
    public Player getOrAdd(String userId) {
        Player player = getByUserId(userId);
        if (player == null){
            UmsMember umsMember = umsMemberService.getById(Integer.valueOf(userId));
            ZmPlayer zmPlayer = new ZmPlayer();
            zmPlayer.setUserId(userId);
            //TODO 用户参赛用户名和手机号不采用微信昵称
//            zmPlayer.setPhone(umsMember.getPhone());
//            zmPlayer.setName(umsMember.getNickname());
            zmPlayer.setCreateTime(new Date());
            zmPlayer.setIntegral(0.00);
            zmPlayer.setIntegralAward(0.00);
            zmPlayer.setLevel(PlayerLevel.LEVEL_0.LEVEL);
            zmPlayer.setRanking(0);
            zmPlayer.setPlayNum(0);
            zmPlayer.setAvatar(umsMember.getAvatar());
            zmPlayerService.save(zmPlayer);
            Player newPlayer = new Player();
            BeanUtils.copyProperties(zmPlayer, newPlayer);
            newPlayer.setLevelTxt(PlayerLevel.getByLevel(zmPlayer.getLevel()).TXT);
            return newPlayer;
        }
        return player;
    }

    @Override
    public boolean updateById(ZmPlayer zmPlayer) {
        return zmPlayerService.updateById(zmPlayer);
    }

    @Override
    public boolean playSuccess(String playerId) {
        ZmPlayer zmPlayer = this.getById(playerId);
        Date now = new Date();
        if (zmPlayer.getPlayNum() == null || zmPlayer.getPlayNum() == 0){
            zmPlayer.setPlayNum(1);
            //新人奖励
            UmsMember umsMember = umsMemberService.getById(zmPlayer.getUserId());
            //奖励推广者积分
            if (ObjectUtils.isNotEmpty(umsMember.getParentUserId()) && umsMember.getParentUserId() > 0){
                UmsMember parentMember = umsMemberService.getById(umsMember.getParentUserId());
                ZmPlayer parentPlayer = this.getByUserId(""+parentMember.getId());
                if (ObjectUtils.isNotEmpty(parentPlayer)){
                    parentPlayer.setIntegralAward(parentPlayer.getIntegralAward()+0.5);
                    parentPlayer.setUpdateTime(now);
                    this.updateById(parentPlayer);
//                    zmPlayer.setUpdateTime(now);
//                    this.updateById(zmPlayer);
                }
            }

        }
        else {
            zmPlayer.setPlayNum(zmPlayer.getPlayNum() + 1);
        }
        zmPlayer.setUpdateTime(now);
        this.updateById(zmPlayer);
        return true;
    }

    @Override
    public boolean playSuccess(String playerId, int playerNum) {
        ZmPlayer zmPlayer = this.getByUserId(playerId);
        if (ObjectUtils.isEmpty(zmPlayer)){
            return false;
        }
        zmPlayer.setUpdateTime(new Date());
        if (playerNum >= 10 && playerNum < 20){
            zmPlayer.setIntegralAward(zmPlayer.getIntegralAward() + 2);
            return this.updateById(zmPlayer);
        }else if (playerNum >= 20){
            zmPlayer.setIntegralAward(zmPlayer.getIntegralAward() + 3);
            return this.updateById(zmPlayer);
        }
        return false;
    }

    private Player fill(ZmPlayer zmPlayer){
        if (ObjectUtils.isEmpty(zmPlayer)){
            return null;
        }
        Player player = new Player();
        BeanUtils.copyProperties(zmPlayer, player);
        player.setLevelTxt(PlayerLevel.getByLevel(player.getLevel()).TXT);
//        ZmPlayerRanking zmPlayerRanking = playerRankingService.getByPlayerId(player.getId());
//        if (ObjectUtils.isNotEmpty(zmPlayerRanking)){
//            player.setIntegral(zmPlayerRanking.getIntegral());
//        }
        return player;
    }

}
